﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using VirtualDrum.lib;
using VirtualDrum.lib.db;
using VirtualDrum.util;

namespace VirtualDrum.componets
{
	public partial class frmMusicInfo : Form
	{
		/// <summary>
		/// 构造函数
		/// </summary>
		public frmMusicInfo()
		{
			InitializeComponent();
			this._actionName = "add";
			this._musicName = "";
			this._oldMusicName = "";
			this._musicPath = "";
			this._musicDrumGroup = "";
		}

		/// <summary>
		/// 返回主窗体
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnMusicReturn_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		/// <summary>
		/// 选择文件按钮事件
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnChoiceFile_Click(object sender, EventArgs e)
		{
			if (DialogResult.OK == this.ofdMusicFile.ShowDialog()) {
				this.lblFilePath.Text = this.ofdMusicFile.FileName;
				this.tbMusicName.Text = LittleTools.getFileName(this.lblFilePath.Text);
			}
		}		

		/// <summary>
		/// 确定添加音源事件
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnMusicSure_Click(object sender, EventArgs e)
		{
			int result = 0;
			this._musicName = this.tbMusicName.Text.Trim();
			this._musicPath = this.lblFilePath.Text.Trim();
			if(false == this.checkMusicInfo()) return;
			if(this._actionName.Equals("add")){
				result	= this.addMusicInfo();
			}else{
				result = this.editMusicInfo();
			}
			if(result > 0){
				MessageBox.Show("操作成功！");
			}else{
				MessageBox.Show("操作失败！");
			}

		}

		/// <summary>
		/// 添加音源信息
		/// </summary>
		/// <returns></returns>
		private int addMusicInfo(){
			string sql = string.Format("INSERT INTO [vd_drummusic] ([musicName],[musicPath],[musicGroupName],[createTime]) values ('{0}','{1}','{2}','{3}');", this._musicName, this._musicPath, this._musicDrumGroup,DBServer.getDateTime());
			return DBServer.executeNonQuery(sql);
		}

		/// <summary>
		/// 编辑音源信息事件 
		/// </summary>
		/// <returns></returns>
		private int editMusicInfo(){
			string sql = string.Format("UPDATE [vd_drummusic] SET [musicName]='{0}',[musicPath]='{1}',[createTime]='{2}' WHERE [musicName] = '{3}';", this._musicName, this._musicPath,DBServer.getDateTime(), this._oldMusicName);
			return DBServer.executeNonQuery(sql);
		}

		/// <summary>
		/// 窗体加载事件
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void frmMusicInfo_Load(object sender, EventArgs e)
		{

			this.lblFilePath.Text = this._musicPath;
			this.tbMusicName.Text = this._musicName;
		}

		/// <summary>
		/// 检测音源信息添加必须项
		/// </summary>
		/// <returns></returns>
		private Boolean checkMusicInfo(){
			if (this._musicName.Equals("") || this._musicPath.Equals(""))
			{
				MessageBox.Show("你有信息项为空！");
				return false;
			}
			return true;
		}

		public string MusicDrumGroup{
			get { return this._musicDrumGroup; }
			set { this._musicDrumGroup = value; }
		}

		public string MusicName{
			get { return this._musicName; }
			set { this._musicName = value; }
		}

		public string MusicPath{
			get { return this._musicPath; }
			set { this._musicPath = value; }
		}

		public string ActionName{
			get { return this._actionName; }
			set { this._actionName = value; }
		}

		public string OldMusicName{
			get { return this._oldMusicName; }
			set { this._oldMusicName = value; }
		}

		private string _actionName;
		private string _musicName;
		private string _oldMusicName;
		private string _musicPath;
		private string _musicDrumGroup;

		private static ControlDatabase DBServer = new DBFactory().createDBServer();

		/// <summary>
		/// 文件路径项点击清除事件
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void lblFilePath_Click(object sender, EventArgs e)
		{
			if(DialogResult.OK == MessageBox.Show("确定清除文件？",frmMain.projectName,MessageBoxButtons.OKCancel)){
				this.lblFilePath.Text = "";
			}
		}
	}
}
